#!/bin/sh
#gitUrl=ssh://git@git.secsbrain.com:10022/EwtFramework/ConfigCenter.git
#gitBranch=master
#sys=net_configcenter
#serverIP=10.0.11.41,10.0.11.46,10.0.11.47
gitUrl=$1
gitBranch=$2
sys=$3
serverIP=$4
codeDir=${sys}.ewt360.com
sourceDir=/cygdrive/d/source
confGitUrl=ssh://git@git.secsbrain.com:10022/ewt_public/conf.git
confGitBranch=$5
scriptsDir=/cygdrive/d/scripts
logsDir=/cygdrive/d/logs
archiveDir=/cygdrive/d/zips
publishDir=/cygdrive/d/publish

# checkResult
checkResult() {
    if [ $? != 0 ];then
        exit 1;
    else
        echo;
    fi
}

if [ ! -d "$sourceDir/$codeDir" ]; then
    cd $sourceDir && git clone $gitUrl $codeDir > $logsDir/${sys}_gitcode.log 2>&1
else
    cd $sourceDir/$codeDir && git checkout . && git clean -xdf && git pull > $logsDir/${sys}_gitcode.log 2>&1
fi
cd $sourceDir/$codeDir && git checkout $gitBranch >> $logsDir/${sys}_gitcode.log 2>&1

envConf=$sourceDir/$codeDir/env.conf
dos2unix $envConf > /dev/null 2>&1
sysName=$(awk -F "=" '/^system_name/{print $2}' $envConf)
confSrcDir=/cygdrive/d/config
webDir=$(awk -F "=" '/^system_web_name/{print $2}' $envConf)
slnDir=$(awk -F "=" '/^system_sln_path/{print $2}' $envConf|sed 's/\.\///g')
confDstDir=$sourceDir/$codeDir/$slnDir/$webDir

if [ ! -d "$confSrcDir/conf" ]; then
    cd $confSrcDir && git clone $confGitUrl > $logsDir/${sysName}_gitconf.log 2>&1
else
    cd $confSrcDir/conf && git pull > $logsDir/${sysName}_gitconf.log 2>&1
fi
cd $confSrcDir/conf && git checkout $confGitBranch >> $logsDir/${sysName}_gitconf.log 2>&1

cp -rf $confSrcDir/conf/$sysName/*.config $confDstDir

slnName=$(awk -F "=" '/^system_sln_name/{print $2}' $envConf)
excludeProjs=$(awk -F "=" '/^system_exclude/{print $2}' $envConf)
excludeProjPool=$(awk 'BEGIN{list="'${excludeProjs}'";split(list,sln_list,",");for(sln in sln_list){print sln_list[sln];}}')

cd $sourceDir/$codeDir/$slnDir
if [ "$excludeProjs" ];then
    echo -n > $logsDir/${sysName}_exclude.log
    for excludeProj in ${excludeProjPool[*]};do
        $scriptsDir/exclude.bat $slnName $excludeProj >> $logsDir/${sysName}_exclude.log 2>&1
    done
fi
$scriptsDir/compile01.bat > $logsDir/${sysName}_compile01.log 2>&1
checkResult;

cd $sourceDir/$codeDir/$slnDir/$webDir
$scriptsDir/compile02.bat $sysName > $logsDir/${sysName}_compile02.log 2>&1
checkResult;

cd $publishDir
if [ -f $archiveDir/$sysName.zip ]; then
    rm -rf $archiveDir/$sysName.zip;
fi
zip -r $archiveDir/$sysName.zip $sysName/ > /dev/null 2>&1

servers=`awk 'BEGIN{list="'${serverIP}'";split(list,ip_list,",");for(ip in ip_list){print ip_list[ip];}}'`
for server in ${servers[*]}
do
    $scriptsDir/trans.bat $server $sysName > $logsDir/${sysName}_${server}.log 2>&1
done
